package com.tybbank.Fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Activity.BuySellListActivity;
import com.tybbank.Activity.FHListActivity;
import com.tybbank.Activity.NewsListActivity;
import com.tybbank.Activity.NoticeActivity;
import com.tybbank.Activity.PrograssbarActivity;
import com.tybbank.Activity.RecordActivity;
import com.tybbank.Activity.UpdatePayPwdActivvity;
import com.tybbank.Activity.UpdatePwdActivvity;
import com.tybbank.Activity.VerifiedActivity;
import com.tybbank.Bean.MeBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.logUtilssss;
import com.tybbank.web.WebActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Administrator on 2018/5/31 0031.
 */

public class MeFragment extends Fragment {


    @BindView(R.id.iv_hand_image)
    ImageView ivHandImage;
    @BindView(R.id.tv_name_phone)
    TextView tvNamePhone;
    @BindView(R.id.rl_prograssbar)
    RelativeLayout rlPrograssbar;
    @BindView(R.id.tv_tisu)
    TextView tvTisu;
    @BindView(R.id.ll_tisu_list)
    LinearLayout llTisuList;
    @BindView(R.id.tv_dingqi)
    TextView tvDingqi;
    @BindView(R.id.ll_dingqi_list)
    LinearLayout llDingqiList;
    @BindView(R.id.bfqqq)
    ImageView bfqqq;
    @BindView(R.id.rl_pwd)
    RelativeLayout rlPwd;
    @BindView(R.id.bf)
    ImageView bf;
    @BindView(R.id.rl_pau_pwd)
    RelativeLayout rlPauPwd;
    @BindView(R.id.b)
    ImageView b;
    @BindView(R.id.rl_real_name)
    RelativeLayout rlRealName;
    @BindView(R.id.qqq)
    ImageView qqq;
    @BindView(R.id.rl_gonggao)
    RelativeLayout rlGonggao;
    @BindView(R.id.qqqw)
    ImageView qqqw;
    @BindView(R.id.rl_help)
    RelativeLayout rlHelp;
    Unbinder unbinder;
    @BindView(R.id.tv_renzheng)
    TextView tvRenzheng;
    @BindView(R.id.tv_fenhong)
    TextView tvFenhong;
    @BindView(R.id.ll_fenhong_list)
    LinearLayout llFenhongList;
    @BindView(R.id.rl_kefu)
    RelativeLayout rlKefu;
    @BindView(R.id.aaa)
    ImageView aaa;
    @BindView(R.id.rl_news)
    RelativeLayout rlNews;
    @BindView(R.id.qqqwq)
    ImageView qqqwq;
    private Intent intent;
    private String renzheng = "";

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_me, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            init();
        }
    }

    private void init() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(getActivity(), getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.user_detail)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG我的" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        MeBean enterBean = new Gson().fromJson(json, new TypeToken<MeBean>() {
                        }.getType());
                        int status = enterBean.getCode();
                        if (status == 1011) {
                            tvTisu.setText(enterBean.getMoney().getAbc_coin());
                            tvDingqi.setText(enterBean.getMoney().getFrozen());
                            tvNamePhone.setText(enterBean.getUser_detail().getAccount());
                            renzheng = enterBean.getUser_detail().getIs_proving();
                            tvFenhong.setText(enterBean.getMoney().getRelease_today());

                            if (enterBean.getUser_detail().getIs_proving().equals("0")) {//is_proving 1已认证 0 未认证
                                ivHandImage.setImageDrawable(getActivity().getResources().getDrawable(R.mipmap.portrait_unauthorized));
                                tvRenzheng.setText(getString(R.string.shiming_no));
                            } else if (enterBean.getUser_detail().getIs_proving().equals("1")) {
                                ivHandImage.setImageDrawable(getActivity().getResources().getDrawable(R.mipmap.portrait_certified));
                                tvRenzheng.setText(getString(R.string.shiming_yes));
                            } else if (enterBean.getUser_detail().getIs_proving().equals("2")) {
                                ivHandImage.setImageDrawable(getActivity().getResources().getDrawable(R.mipmap.efault_authen));
                                tvRenzheng.setText(getString(R.string.shiming_loging));
                            }
                        } else if (status == 1004) {
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                            return;
                        } else {
                            Toast.makeText(getActivity(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_news, R.id.rl_kefu, R.id.ll_fenhong_list, R.id.rl_prograssbar, R.id.ll_tisu_list, R.id.ll_dingqi_list, R.id.rl_pwd, R.id.rl_pau_pwd, R.id.rl_real_name, R.id.rl_gonggao, R.id.rl_help})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_prograssbar:   //个人中心
                intent = new Intent(getActivity(), PrograssbarActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.ll_tisu_list: //可用值流水
                intent = new Intent(getActivity(), RecordActivity.class);
                intent.putExtra("type", "3");
                intent.putExtra("classify", "0");
                intent.putExtra("title", getString(R.string.jilu));
                getActivity().startActivity(intent);
                break;
            case R.id.rl_news:  //新闻
                // Intent  intent = new Intent(getActivity(), NoticeActivity.class);
                intent = new Intent(getActivity(), NewsListActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.ll_fenhong_list:   //分红值
                intent = new Intent(getActivity(), FHListActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_dingqi_list:   // 提速值流水]
                intent = new Intent(getActivity(), BuySellListActivity.class);
                intent.putExtra("type", "2");
                getActivity().startActivity(intent);
                break;
            case R.id.rl_pwd:   // 安全密码
                intent = new Intent(getActivity(), UpdatePwdActivvity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.rl_pau_pwd:   //支付密码
                intent = new Intent(getActivity(), UpdatePayPwdActivvity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.rl_real_name: //实名认证
                intent = new Intent(getActivity(), VerifiedActivity.class);
                startActivityForResult(intent, 200);
                break;
            case R.id.rl_gonggao:   //公告
                intent = new Intent(getActivity(), NoticeActivity.class);
                intent.putExtra("type", "2");
                intent.putExtra("title", getString(R.string.gonggao));
                getActivity().startActivity(intent);
                break;
            case R.id.rl_help:  //帮助中心
                intent = new Intent(getActivity(), NoticeActivity.class);
                intent.putExtra("type", "3");
                intent.putExtra("title", getString(R.string.changjian));
                getActivity().startActivity(intent);
                break;
            case R.id.rl_kefu:  //客服
                intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("title", getString(R.string.kefu));
                intent.putExtra("url", Constant.KEFU);
                getActivity().startActivity(intent);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200 && resultCode == RESULT_OK) {
            init();

        }
    }


}
